import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/entity/entities/lf_user_entity.dart';
import 'package:largefriends/pages/lf_base_controller.dart';
import 'package:largefriends/pages/lf_manage_access/lf_manage_access_loading.dart';
import 'package:largefriends/pages/lf_manage_access/lf_manage_access_logic.dart';
import 'package:largefriends/res/lf_color.dart';
import 'package:largefriends/res/lf_double_ext.dart';
import 'package:largefriends/res/lf_fonts.dart';
import 'package:largefriends/res/lf_text_style.dart';
import 'package:largefriends/res/lf_widget_ext.dart';
import 'package:largefriends/res/strings/lf_strings.dart';
import 'package:largefriends/widgets/common_empty_page.dart';
import 'package:largefriends/widgets/common_failed_page.dart';
import 'package:largefriends/widgets/custom_cache_image.dart';
import 'package:largefriends/widgets/lf_app_bar.dart';

class LfManageAccessPage extends StatefulWidget {
  const LfManageAccessPage({super.key});

  @override
  State<LfManageAccessPage> createState() => _LfManageAccessPageState();
}

class _LfManageAccessPageState extends State<LfManageAccessPage> {
  final logic = Get.put(LfManageAccessLogic());
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        child: Scaffold(
          appBar: lfAppBar(
            title: strManageAccess.tr,
            leadingCallback: () {
              Get.back();
            },
          ),
          body: GetBuilder<LfManageAccessLogic>(
            builder: (controller) {
              if (controller.dataList.isEmpty) {
                if (controller.state == LoadingState.loading ||
                    controller.state == LoadingState.init) {
                  return const LfManageAccessLoading();
                } else if (controller.state == LoadingState.failed) {
                  return CommonFailedPage(retry: () {
                    controller.getCustomParamHttpRequest(isRefresh: true);
                  });
                } else {
                  return LayoutBuilder(builder: (context, constant) {
                    return CommonEmptyPage(
                      title: strNoMembersYet.tr,
                      tip: strUploadPrivatePhotosTip.tr,
                      constraints: constant,
                    );
                  });
                }
              }

              return EasyRefresh(
                  child: CustomScrollView(
                slivers: [
                  ...controller.dataList.map((e) => _item(e).sliverBox)
                ],
              ));
            },
          ),
        ),
        onWillPop: () async {
          Get.back();
          return false;
        });
  }

  Widget _item(LfUserEntity user) {
    List<Widget> actionButton = List.empty(growable: true);
    if (user.isBlockMe() == false && user.isHiddenForMe() == false) {
      if (user.canAccessMyPrivate == 2) {
        actionButton.add(_acceptWidget(user));
        actionButton.add(40.w.sbw);
        actionButton.add(_declineWidget(user));
      } else {
        actionButton.add(_revokeWidget(user));
      }
    } else {
      if (user.canAccessMyPrivate == 2) {
        actionButton.add(_declineWidget(user));
      } else {
        actionButton.add(_revokeWidget(user));
      }
    }
    if (actionButton.isEmpty) {
      return empty;
    }
    return Container(
      margin: EdgeInsets.only(left: 10.w, right: 10.w, top: 15.h),
      padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 10.h),
      decoration:
          BoxDecoration(color: color8F8F8F, borderRadius: 10.r.radius),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(
            height: 80.h,
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                CustomCacheImage(
                  imageUrl: user.avatar,
                  width: 80.r,
                  height: 80.r,
                  radius: 8.r,
                ),
                8.w.sbw,
                Expanded(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        user.username.toUpperCase(),
                        style: lfTextStyle(sp: 18.sp, fontFamily: fontMedium),
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      ),
                      Text(
                        user.getGenderWithAgeLiveIn(),
                        style: lfTextStyle(
                            sp: 16.sp,
                            fontFamily: fontRegular,),
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
          10.h.sbh,
          Row(
            children: actionButton,
          )
        ],
      ),
    );
  }

  Widget _acceptWidget(LfUserEntity user) {
    return InkWell(
      child: Text(
        strAccept.tr,
        style: lfTextStyle(
            sp: 18.sp,
            fontFamily: fontBold,
            decoration: TextDecoration.underline,),
      ),
      onTap: () {
        logic.accessPrivate(user);
      },
    );
  }

  Widget _declineWidget(LfUserEntity user) {
    return InkWell(
      child: Text(
        strDecline.tr,
        style: lfTextStyle(
            sp: 18.sp,
            fontFamily: fontBold,
            decoration: TextDecoration.underline),
      ),
      onTap: () {
        logic.declinedPrivate(user);
      },
    );
  }

  Widget _revokeWidget(LfUserEntity user) {
    return InkWell(
      child: Text(
        strRevoke.tr,
        style: lfTextStyle(
            sp: 18.sp,
            fontFamily: fontMedium,
            decoration: TextDecoration.underline),
      ),
      onTap: () {
        logic.revokePrivate(user);
      },
    );
  }
}
